//
//  JudgeStarsView.m
//  yjw-winner
//
//  Created by 隆霁 on 2018/9/18.
//  Copyright © 2018年 Shanghai decade financial information service co., LTD. All rights reserved.
//

#import "JudgeStarsView.h"

@interface JudgeStarsView() {
    NSArray *_markImageArray; // view
    NSArray *_markStarArray; // 亮星
    int _starLelvel; // 星级
}

@end
@implementation JudgeStarsView
@synthesize stars = _stars;

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setupContentViews];
}
- (instancetype)init {
    if (self = [super init]) {
        [self setupContentViews];
    }
    return self;
}
- (void)setStars:(CGFloat)stars {
    _stars = stars;
    [self setStarsImageViewWith:(int)stars * 2];
}

/**
 初始化view
 */
- (void)setupContentViews {
    UIImage *bgImage = [UIImage imageNamed:@"un_judge_star_default"];
    UIImage *image_l = [UIImage imageNamed:@"un_judge_star_l"];
    UIImage *image_r = [UIImage imageNamed:@"un_judge_star_r"];
    
    NSMutableArray *mArr = [NSMutableArray arrayWithCapacity:5];
    NSMutableArray *mStars = [NSMutableArray arrayWithCapacity:10];
    for (int i = 0; i < 5; i++) {
        UIView *v = [[UIView alloc] init];
        v.userInteractionEnabled = NO;
        UIImageView *imageView = [[UIImageView alloc] initWithImage:bgImage];
        imageView.clipsToBounds = YES;
        imageView.contentMode = UIViewContentModeScaleToFill;
        [v addSubview:imageView];
        for (int j = 0; j < 2; j++) {
            UIImageView *i = [[UIImageView alloc] initWithImage:((j % 2 == 0) ? image_l : image_r)];
            i.clipsToBounds = YES;
            i.contentMode = UIViewContentModeScaleToFill;
            [v addSubview:i];
            [mStars addObject:i];
        }
        [mArr addObject:v];
        [self addSubview:v];
    }
    _markImageArray = [NSArray arrayWithArray:mArr];
    _markStarArray = [NSArray arrayWithArray:mStars];
}

/**
 重新布局frame
 */
- (void)layoutSubviews {
    [super layoutSubviews];
    CGFloat w = self.mj_w;
    CGFloat h = self.mj_h;
    CGFloat l = MIN(h, (w - h) * 0.2); // subviews 间距为 高的 1/4 倍
    for (int i = 0; i < _markImageArray.count; i++) {
        UIView *v = _markImageArray[i];
        v.frame = CGRectMake(i * (l + h * 0.25), 0, l, l);
        UIImageView *imageView = v.subviews.firstObject;
        imageView.frame = v.bounds;
    }
    for (int i = 0; i < _markStarArray.count; i++) {
        UIImageView *imageView = _markStarArray[i];
        if ((i % 2 == 0)) {
            imageView.frame = CGRectMake(0, 0, l * 0.5, l);
        }else {
            imageView.frame = CGRectMake(l * 0.5, 0, l * 0.5, l);
        }
    }
}

/**
 设置星级
 */
- (void)setStarsImageViewWith:(int)starsLevel {
    if (starsLevel == _starLelvel) {
        return;
    }
    _starLelvel = starsLevel;
    for (int i = 0; i < _markStarArray.count; i++) {
        UIImageView *imageView = _markStarArray[i];
        imageView.hidden = (i >= starsLevel);
    }
}

/**
 根据坐标设置星级

 @param touch touch
 */
- (void)setStarsImageViewWithTouch:(UITouch *)touch {
    for (UIImageView *imageView in _markStarArray) {
        CGRect rect = [imageView convertRect:imageView.frame toView:self];
        BOOL touchInRect = CGRectContainsPoint(CGRectMake(0, 0, CGRectGetMaxX(rect), self.mj_h), [touch locationInView:touch.view]);
        imageView.hidden = touchInRect;
    }
    NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(id  _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
        return ![(UIImageView *)evaluatedObject isHidden];
    }];
    NSArray *arr = [_markStarArray filteredArrayUsingPredicate:predicate];
    self.stars = (arr.count / 2);
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch *touch = touches.anyObject;
    CGFloat last_touch_center_x = [touch locationInView:touch.view].x;
    NSLog(@"%s\n%lf\n",__func__ ,last_touch_center_x);
    [self setStarsImageViewWithTouch:touch];
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch *touch = touches.anyObject;
    CGFloat last_touch_center_x = [touch locationInView:touch.view].x;
    NSLog(@"%s\n%lf\n",__func__ ,last_touch_center_x);
    [self setStarsImageViewWithTouch:touch];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch *touch = touches.anyObject;
    CGFloat last_touch_center_x = [touch locationInView:touch.view].x;
    NSLog(@"%s\n%lf\n",__func__ ,last_touch_center_x);
    [self setStarsImageViewWithTouch:touch];
}
@end
